package session.TX;

import java.util.Calendar;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import javax.ejb.Stateless;
import entity.*;
import java.util.List;
import javax.persistence.Query;

@Stateless
public class TransactionManagerSB implements TransactionManagerSBRemote {

    @PersistenceContext
    EntityManager em;
    private TransactionEntry tranEntity;
    private PurchasedItem_Regular itemEntity;

    public TransactionManagerSB() { //no-arg constructor
    }

    @Override
    public void createTransactionEntry(
            Calendar time_stamp,
            String customerEmail,
            Integer loyaltyPointsAwarded,
            Double subtotal,
            List<PurchasedItem> items) {
        tranEntity = new TransactionEntry();

        //persist the purchase items into the database
        for (PurchasedItem p_item : items) {
            em.persist(p_item);
        }

        tranEntity.setTime_stamp(time_stamp);
        tranEntity.setCustomerEmail(customerEmail);
        tranEntity.setLoyaltyPointsAwarded(loyaltyPointsAwarded);
        tranEntity.setSubtotal(subtotal);
        tranEntity.setItems(items);

        em.persist(tranEntity);
        
        
        //Update the most recent transaction and the loyalty points for the customer
        Query q = em.createQuery("SELECT c from Users c where c.email = :paramCustomerEmail ");
        q.setParameter("paramCustomerEmail", customerEmail);
        
        Users customer = (Users) q.getSingleResult();
        if (customer == null) {
            System.out.println(">>> TransactionManagerBean: user not found");
            return;
        }
        
        
        Integer points = customer.getLoyaltypoint() + loyaltyPointsAwarded;
        customer.setMostRecentTransactionDate(time_stamp.getTime());
        customer.setLoyaltypoint(points);
        
        
                    
        //add the transaction into the user's list of transactions
        List<TransactionEntry> temp = customer.getList_transactionEntry();
        temp.add(tranEntity);
        customer.setList_transactionEntry(temp);
                    
        
        
        em.flush();
        
        System.out.println(">>> TransactionManagerBean: Created Transaction Entry");
    }//-------------------------------------------------------------------------------------------------------

    @Override
    public PurchasedItem createPurchasedItem_Regular(
            Calendar time_stamp,
            String customerEmail,
            String seller,
            String item_name,
            Long item_id,
            Double item_total_price,
            String buyer_country,
            Integer buyer_age,
            Long outlet_id) {

        itemEntity = new PurchasedItem_Regular();

        itemEntity.setTime_stamp(time_stamp);
        itemEntity.setCustomerEmail(customerEmail);
        itemEntity.setSeller(seller);
        itemEntity.setItem_name(item_name);
        itemEntity.setItem_id(item_id);
        itemEntity.setSubtotal(item_total_price);
        itemEntity.setBuyer_country(buyer_country);
        itemEntity.setBuyer_age(buyer_age);
        itemEntity.setOutlet_id(outlet_id);

        return itemEntity;

    }//-------------------------------------------------------------------------------------------------------

    //Xiaodi added on 19:22, 18/10/2011
    public PurchasedItem createPurchasedItem_GroupOn(Long id,
            Calendar time_stamp,
            String customerEmail,
            String seller,
            String creditCard,
            Long customerId,
            Integer qty,
            Double subtotal) {
        PurchasedItem_GroupOn pbItem = new PurchasedItem_GroupOn();
        pbItem.setTime_stamp(time_stamp);
        pbItem.setCustomerEmail(customerEmail);
        pbItem.setSeller(seller);
        pbItem.setCreditCard(creditCard);
        pbItem.setCustomerId(customerId);
        pbItem.setQty(qty);
        pbItem.setSubtotal(subtotal);
        return pbItem;
    }
}
